/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.system;


import com.tlf.dyjc5.service.PointService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.web.util.CommonWebUtil;
import org.dusin.web.util.JsonUtil;
import org.dusin.web.util.TheUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class PointLoad extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String jsonText = null;
        try {
            /* TODO output your page here. You may use following sample code. */
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            PointService pointService = (PointService) context.getBean("pointService");

            String command = request.getParameter("cmd");
            List<Map<String, Object>> list = null;
            String currentOrgCode = (String) request.getSession().getAttribute("currentOrgCode");
            int start = CommonWebUtil.getIntParameter(request.getParameter("start"),0);
            int limit = CommonWebUtil.getIntParameter(request.getParameter("limit"),20);
            if ("general".equals(command)) {              
                String cond = TheUtil.getPointSelectCondition(request);
                //String dateTo = request.getParameter("dateTo");
                //System.out.println("cond"+cond);
                String loadCmd = request.getParameter("loadCmd");
                if("history".equals(loadCmd)){
                    list = pointService.findAllDeleted(currentOrgCode, cond,start, limit);
                }else{
                    list = pointService.findAll(currentOrgCode, cond,start, limit);
                }                
            }if ("report".equals(command)) {              
                String cond = TheUtil.getPointSelectCondition(request);
                String dateTo = request.getParameter("dateTo");
                //System.out.println("cond"+cond);
                list = pointService.findAllStartedByMonth(currentOrgCode,dateTo, cond,start, limit);
            } else if ("param".equals(command)) {
                String cond = TheUtil.getPointSelectCondition(request);                
                list = pointService.findAllParams(currentOrgCode,cond+" and deviceId>0", start, limit);
            }else if("run".equals(command)){
                String cond = TheUtil.getPointSelectCondition(request);
                list = pointService.findAllRuns(currentOrgCode,cond+" and deviceId>0", start, limit);
            }else if("frompoint".equals(command)){
                String cond = TheUtil.getPointSelectCondition(request);
                list = pointService.findAllFromPoint(currentOrgCode,cond+" and deviceId>0", start, limit);
            }else if("connection".equals(command)){   //关联点
                String cond = TheUtil.getPointSelectCondition(request);
                list = pointService.findAllConnection(currentOrgCode,cond, start, limit);
            }else if("superior".equals(command)){   //上级关联点
                long id = Long.parseLong(request.getParameter("id"));
                list = pointService.findAllSuperior(id);
            }else if("sibling".equals(command)){   //同级关联点
                long id = Long.parseLong(request.getParameter("id"));                
                list = pointService.findAllSibling(id);
            }else if("junior".equals(command)){   //下级关联点
                long id = Long.parseLong(request.getParameter("id"));                
                list = pointService.findAllJunior(id);
            }else if("workStatus".equals(command)){                
                list=pointService.findAllWorkStatus(currentOrgCode,currentOrgCode.length()+2);
            }else if("number".equals(command)){
                
                int reportStatus = Integer.parseInt(request.getParameter("reportStatus"));
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String date = request.getParameter("dateTo");
                list=pointService.findNumberOfType(currentOrgCode, currentOrgCode.length(),date,reportStatus,range);
                
            }else if("subnumber".equals(command)){
                
                int reportStatus = Integer.parseInt(request.getParameter("reportStatus"));
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String date = request.getParameter("dateTo");
                list=pointService.findNumberOfType(currentOrgCode,currentOrgCode.length()+2,date,reportStatus,range);
                
            }else if("number2".equals(command)){      //单位覆盖率
                String currentOrgName = (String)request.getSession().getAttribute("currentOrgName");
                int reportStatus = Integer.parseInt(request.getParameter("reportStatus"));
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String dateTo = request.getParameter("dateTo");
                list=pointService.findCoverageRate(currentOrgCode, dateTo,reportStatus,range);
                Map map = list.get(0);
                map.put("company", currentOrgName);
            }else if("number3".equals(command)){      //单位实测率
                String currentOrgName = (String)request.getSession().getAttribute("currentOrgName");
                int reportStatus = Integer.parseInt(request.getParameter("reportStatus"));
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String dateTo = request.getParameter("dateTo");
                String lock = request.getParameter("lock");
                list=pointService.findUploadRate(currentOrgCode, dateTo,reportStatus,range,lock);
                Map map = list.get(0);
                map.put("company", currentOrgName);
            }else if("number4".equals(command)){      //综合展示-〉测点汇总信息,监测点分布
                int type = Integer.parseInt(request.getParameter("type"));
                int reportStatus = CommonWebUtil.getIntParameter(request.getParameter("reportStatus"), -1);
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String date = request.getParameter("dateTo");
                String quarter = request.getParameter("quarterTo");
                switch(type){
                    case 1:
                        list=pointService.findNumberOfTypeByDay(currentOrgCode,currentOrgCode.length()+2,date,reportStatus,range);
                        break;
                    case 2:
                        list=pointService.findNumberOfType(currentOrgCode,currentOrgCode.length()+2,date.substring(0,6),reportStatus,range);
                        break;
                    case 3:
                        list=pointService.findNumberOfTypeByQuarter(currentOrgCode,currentOrgCode.length()+2,date.substring(0,4)+quarter,reportStatus,range);
                        break;
                    case 4:
                        list=pointService.findNumberOfTypeByYear(currentOrgCode,currentOrgCode.length()+2,date.substring(0,4),reportStatus,range);
                        break;
                    default:
                        list=pointService.findNumberOfType(currentOrgCode,currentOrgCode.length()+2,date.substring(0,6),reportStatus,range);                        
                }                
                
            }else if("displayPoint".equals(command)){      //综合展示-〉测点汇总信息,监测点分布
                int type = Integer.parseInt(request.getParameter("type"));
                
                //int reportStatus = CommonWebUtil.getIntParameter(request.getParameter("reportStatus"), -1);
                //int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String cond = TheUtil.getPointSelectCondition(request);
                String date = request.getParameter("dateTo");
                String quarter = request.getParameter("quarterTo");
                switch(type){
                    case 1:
                        list=pointService.findAllStartedByDay(currentOrgCode,date,cond,start,limit);
                        break;
                    case 2:
                        list=pointService.findAllStartedByMonth(currentOrgCode,date.substring(0,6),"",start,limit);
                        break;
                    case 3:
                        list=pointService.findAllStartedByQuarter(currentOrgCode,date.substring(0,4)+quarter,"",start,limit);
                        break;
                    case 4:
                        list=pointService.findAllStartedByYear(currentOrgCode,date.substring(0,4),"",start,limit);
                        break;
                    default:
                        list=pointService.findAllStartedByMonth(currentOrgCode,date.substring(0,6),"",start,limit);
                }                
                
            }else if("5m".equals(command)){
                String cond = TheUtil.getPointSelectCondition(request);
                list=pointService.findAll5m(currentOrgCode, cond, start, limit);
            }else if("distribution".equals(command)){
                int reportStatus = CommonWebUtil.getIntParameter(request.getParameter("reportStatus"), -1);
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                
                list = pointService.findAllDistribution(currentOrgCode,currentOrgCode.length()+2,dateFrom,dateTo,reportStatus,range);
            }else if("change".equals(command)){   //变更历史
                String cond = TheUtil.getPointSelectCondition(request);
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                cond = cond + " and (createTime>='"+dateFrom+"' and " + "createTime<'" +dateTo+" 23:59:59') ";
                list=pointService.findAllHistory(currentOrgCode, cond, start, limit);
            }else if("changeSummary".equals(command)){   //变更统计
                
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                
                list=pointService.findAllHistorySummary(currentOrgCode,currentOrgCode.length(),dateFrom,dateTo,-1,-1);
            }else if("typeSummary".equals(command)){   //运维统计 
                
                list=pointService.findAllCurrentStartedNumberVertical(currentOrgCode, 1, -1);
            }else if("checkSummary".equals(command)){                   //告警事件->告警查询统计
                //String cond = TheUtil.getPointSelectCondition(request);
                int reportStatus = CommonWebUtil.getIntParameter(request.getParameter("reportStatus"), -1);
                int range = CommonWebUtil.getIntParameter(request.getParameter("range"), -1);
                int days = CommonWebUtil.getIntParameter(request.getParameter("days"), 7);
                list = pointService.findAllCheckSummary(currentOrgCode,currentOrgCode.length(),days,reportStatus,range);
            }else if("expireWarn".equals(command)){
                String cond = TheUtil.getPointSelectCondition(request);
                cond += " and datediff(day,getDate(),expireTime)<0";
                list = pointService.findAllCheckExpired(currentOrgCode, cond, start, limit);
            }
            
            jsonText = JsonUtil.JavaMapListToGridFormat("data", list, pointService.getSize());
            
        } catch (Exception e) {
            jsonText = JsonUtil.createFailureJsonResponse(e.getMessage());
            e.printStackTrace();
        } finally {
            out.print(jsonText);
            out.flush();
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
